package org.vt.ece3574.pictoslider.menus;

import org.vt.ece3574.pictoslider.R;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;

public class Themes {

	/**
	 * The position of the theme to switch to.
	 */
	private static int themePos;

	/**
	 * String Key used in SharedPreferences. Used to store an integer
	 * representing the Custom theme background color.
	 */
	public static final String BACKGROUND_COLOR = "bgColor";
	/**
	 * String Key used in SharedPreferences. Used to store an integer
	 * representing the Custom theme Text Color.
	 */
	public static final String TEXT_COLOR = "txtColor";

	// Position of themes in the spinner
	private static final int BLACK_POS = 0;
	private static final int LIGHT_POS = 1;
	private static final int HOKIE_POS = 2;
	private static final int SKY_POS = 3;
	public static final int CUSTOM_POS = 4;

	// Hokie Theme colors
	private static final int HOKIE_MAROON = Color.rgb(102, 0, 0);
	private static final int HOKIE_ORANGE = Color.rgb(255, 102, 0);

	// Sky Theme colors
	private static final int SKY_BLUE = Color.rgb(107, 151, 189);
	private static final int SKY_WHITE = Color.WHITE;

	/**
	 * Defualt Theme to be used by application.
	 */
	public static final int DEFAULT_THEME = BLACK_POS;

	/**
	 * Private constructor for this class ensures that there is only one
	 * instance of Themes()
	 */
	private Themes() {
	}

	/**
	 * @param pos
	 *            - Position selected in Theme Spinner.
	 */
	public static void setThemePos(int pos) {
		themePos = pos;
	}

	/**
	 * @return The resource id for the current theme.
	 */
	public static int getTheme() {
		return positionToTheme(themePos);
	}

	/**
	 * Dynamically changes the activity's theme.
	 * 
	 * @param activity
	 *            - The calling activity.
	 * @param theme
	 *            - The position in the spinner of the theme to change to.
	 */
	public static void changeToTheme(Activity activity, int theme) {
		themePos = theme;
		activity.finish();
		activity.startActivity(new Intent(activity, activity.getClass()));
	}

	/**
	 * To be called by an activity in onCreate.
	 * 
	 * @param activity
	 */
	public static void onActivityCreateSetTheme(Activity activity) {
		if (themePos != CUSTOM_POS) {
			activity.getApplicationContext()
					.setTheme(positionToTheme(themePos));
			activity.setTheme(positionToTheme(themePos));
		} else {
			activity.getApplicationContext().setTheme(
					android.R.style.Theme_Black);
			activity.setTheme(android.R.style.Theme_Black);
		}
	}

	/**
	 * To be called by an activity after onActivityCreateSetTheme
	 * 
	 * @param activity
	 * @param id
	 */
	public static void onActivityCreateSetContentView(Activity activity, int id) {
		activity.setContentView(id);
		View v = activity.findViewById(R.id.toplevel);
		setBackgroundColor(activity, v);
	}

	/**
	 * Set the given view in the given activity to the background color stored
	 * in SharedPreferences.
	 * 
	 * @param activity
	 *            - The Activity containing the view to color.
	 * @param v
	 *            - The view to color.
	 */
	public static void setBackgroundColor(Activity activity, View v) {
		if (themePos == CUSTOM_POS) {
			v.setBackgroundColor(activity.getSharedPreferences(
					SettingsMenu.SETTINGS_PREFS, 0).getInt(BACKGROUND_COLOR, 0));
		} else if (themePos == HOKIE_POS) {
			v.setBackgroundColor(HOKIE_MAROON);
		} else if (themePos == SKY_POS) {
			v.setBackgroundColor(SKY_BLUE);
		} else if (themePos == LIGHT_POS) {
			v.setBackgroundColor(Color.WHITE);
		}
	}

	/**
	 * @param activity
	 *            - The current Activity.
	 * @return The text color used by the current theme.
	 */
	public static int textColor(Activity activity) {
		switch (themePos) {
		case HOKIE_POS:
			return HOKIE_ORANGE;
		case LIGHT_POS:
			return Color.BLACK;
		case BLACK_POS:
			return Color.WHITE;
		case SKY_POS:
			return SKY_WHITE;
		case CUSTOM_POS:
			return activity
					.getSharedPreferences(SettingsMenu.SETTINGS_PREFS, 0)
					.getInt(TEXT_COLOR, Color.WHITE);
		}
		return Color.WHITE;
	}

	/**
	 * Converts a position in the Theme Spinner to the appropriate theme
	 * 
	 * @param pos
	 *            - The position of the item in the spinner.
	 * @return The id of the android theme.
	 */
	public static int positionToTheme(int pos) {
		switch (pos) {
		case HOKIE_POS:
			return R.style.Hokies;
		case LIGHT_POS:
			return R.style.Light_BlackText;
		case BLACK_POS:
			return android.R.style.Theme_Black;
		}
		return android.R.style.Theme_Black;
	}
}
